package com.ksm.zyz.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.ksm.zyz.common.base.BaseController;
import com.ksm.zyz.common.base.Global;
import com.ksm.zyz.sys.entity.*;
import com.ksm.zyz.sys.service.SysPermissionService;
import com.ksm.zyz.sys.service.SysRolePermService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author litao
 *
 */
@Controller
@RequestMapping("perm")
public class SysRolePermController extends BaseController<SysRolePermission> {
	
	@Autowired//自动set
	private SysRolePermService sysRolePermService;

	@Autowired//自动set
	private SysPermissionService sysPermissionService;
	private static Logger logger = LoggerFactory.getLogger(SysRolePermController.class);


	/**
	 * 查询全部资源
	 *
	 * @param sysPermission
	 * @param req
	 * @return
	 */
	@RequestMapping("list")
	@ResponseBody
	public ExtData selectAll(SysPermission sysPermission, HttpServletRequest req) {
		sysPermission.setBsData(getBsData(req));
		extData.setList(sysPermissionService.selectAll(sysPermission));
		extData.setTotalCount(sysPermissionService.sysselectCount(sysPermission));
		return extData;
	}

	/**
	 * 资源新增
	 *
	 * @param sysPermission
	 * @return
	 */
	@RequestMapping(value = "create",method = RequestMethod.POST)
	@ResponseBody
	public Message create(SysPermission sysPermission) {
		sysPermissionService.save(sysPermission);
		return new Message(Global.SUCCESSFUL);
	}

	/**
	 * 资源修改
	 *
	 * @param sysPermission
	 * @return
	 */
	@RequestMapping("update")
	@ResponseBody
	public Message update(SysPermission sysPermission) {
		sysPermissionService.update(sysPermission);
		return new Message(Global.SUCCESSFUL);
	}

	/**
	 * 资源删除
	 *
	 * @param ids
	 * @return
	 */
	@RequestMapping("delete")
	@ResponseBody
	public Message delete(String ids) {
		String[] deptids = ids.split(",");
		sysPermissionService.delete(deptids);
		return new Message(Global.SUCCESSFUL);
	}

	/**
	 * 权限查找
	 * @return
	 */
	@RequestMapping("getPermission")
	@ResponseBody
	public List<CheckBox> selectRight(String roleId){

		List<CheckBox> jsonList = sysRolePermService.getPermission(roleId);
		List<CheckBox> list = new ArrayList<CheckBox>();
		for(int i=0;i<jsonList.size();i++){
			String id = jsonList.get(i).getId();
			for(CheckBox j2 : jsonList){
				if(id.equals(j2.getParentKey())){
					jsonList.get(i).setLeaf(false);
					j2.setLeaf(true);
					jsonList.get(i).getChildren().add(j2);
				}
			}
		}
		for(CheckBox j2 : jsonList){
			String key = j2.getParentKey();
			if(key==null||"".equals(key)){
				list.add(j2);
			}
		}
		return list;
	}
	
	/**
	 * 权限保存
	 * @param req
	 * @return
	 * @throws IOException 
	 * @throws  
	 * @throws  
	 */
	@RequestMapping("savePermission")
	@ResponseBody
	public Message savePermission(HttpServletRequest req) throws IOException{
		String[] roleIds = {req.getParameter("roleId")};
		String permissions = req.getParameter("permission");

		ObjectMapper mapper = new ObjectMapper();
		List<SysRolePermission> rightList = mapper.readValue(permissions, List.class);
		sysRolePermService.delete(roleIds);
		sysRolePermService.savePermission(rightList);
		return new Message(Global.SUCCESSFUL);
	
	}
	
	/**
	 * 按钮查询
	 * @return
	 */
	@RequestMapping("selectButton")
	@ResponseBody
	public Message selectButton(String roleId,String parentId){
		List<SysPermission> btnList = sysRolePermService.selectButton(roleId, parentId);
		return new Message(btnList);
	}
}
